package pers.fulsun.blog.controller;

import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.common.support.IController;
import net.dreamlu.mica.core.result.R;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import pers.fulsun.blog.entity.Category;
import pers.fulsun.blog.service.CategoryService;

/**
 * 服务控制器
 *
 * @author fl
 * @since 2021-11-29 11:19:42
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/category")
public class CategoryController implements IController {
  private final CategoryService categoryService;

  @RequestMapping(value = "/", method = RequestMethod.POST)
  public R<String> addNewCate(Category category) {

    if (StringUtils.isEmpty(category.getCateName())) {
      return fail("请输入栏目名称!");
    }

    if (categoryService.addCategory(category)) {
      return success("添加成功!");
    }
    return success("添加失败!");
  }

  @RequestMapping(value = "/all", method = RequestMethod.GET)
  public List<Category> getAllCategories() {
    return categoryService.getAllCategories();
  }
}
